package com.itheima.exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) {    //1.加throws ParseException

        //异常

        //test1();

        //2.try-catch
        /*try {
            test2();
        } catch (ParseException e) {
            e.printStackTrace();        //打印异常信息
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
        try {
            test2();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test2() throws Exception {    //ParseException, FileNotFoundException

        //编译时异常：编译阶段就会出现的异常，如日期解析异常
        System.out.println("开始");
        String str = "2024/08-22 16:25:33";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //将字符串时间解析成日期对象
        Date date = sdf.parse(str);         //编译时异常，提醒此处易出错
        System.out.println(date);

        InputStream is = new FileInputStream("hello.txt");

        System.out.println("结束");

    }

    public static void test1() {

        //运行时异常：编译时不会报错，运行时才会出现的异常，继承自RuntimeException

        /*System.out.println("开始");
        int[] arr = {1,2,3};
        System.out.println(arr[10]); */       //ArrayIndexOutOfBoundsException

        String str = null;
        //System.out.println(str);                 //NullPointerException
        System.out.println(str.length());       //NullPointerException

        System.out.println("结束");       //不会执行

    }
}
